<?php
/*
 *      test.php
 *      
 *      Copyright 2011 ViRak <vetalrakitin@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

require_once($_SERVER['DOCUMENT_ROOT']."/classes/metadata/osmetadata.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/osdata.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/system.php");
session_start();

if (!array_key_exists("metadata", $_SESSION)) {
	$_SESSION["metadata"] = new osmetadata();
} else {
	if (!(get_class($_SESSION["metadata"]) == "osmetadata")) {
		$_SESSION["metadata"] = new osmetadata();
	}
}
	$metadata = &$_SESSION["metadata"];
	
if (!array_key_exists("data", $_SESSION)) {
	$_SESSION["data"] = new osdata();
} else {
	if (!(get_class($_SESSION["data"]) == "osdata")) {
		$_SESSION["data"] = new osdata();
	}
}
	$data = &$_SESSION["data"];
	
	$metadata->openfromfile("/home/vetal/openshift/meta.conf");

class osuserinterface {
	
	private $components = array();
	
	function __construct() {
		$_SESSION["userinterface"] = $this;
		$login = new oslogin(0);
/*
		if ($login->active) {
			echo "Интерфейс пользователя";
		} else {
*/
			echo $login->get();
/*
		}
*/
	}
	
	function addcomponent($component, $parent) {
		$this->components[$component->uuid] = $component;
	}
	
	function message($component, $function, $param1, $param2) {
		$this->components[$component]->{$function}($param1,$param2);
		//echo $function;
	}
	
	function destroycomponent($component) {
		unset($this->components[$component->uuid]);
	}
}
	
class oscomponent {
	
	public $uuid;
	protected $active = true;
	private $hooks = array();
	
	function __construct($parent) {
		$this->uuid = genuuid();
		$_SESSION["userinterface"]->addcomponent($this, $parent);
	}
	
	function close() {
		$_SESSION["userinterface"]->destroycomponent($this);
		unset($this);
	}
	
	function __set($name,$value) {
		switch ($name) {
			case "dd":break;
		}
	}
	
	function enable() {
		$this->active = true;
		$this->update();
	}
	
	function disable() {
		$this->active = false;
		$this->update();
	}
	
	function __get($name) {
		switch ($name) {
			case "active":{return $this->active;break;}
		}
	}
	
	function setonchange($object, $func) {
		$this->hooks["onchange"][$func] = $object;
	}
	
	function setevent($object, $event, $func) {
		$this->hooks[$event][$func] = $object;
	}
	
	function onchange($value) {
		if (array_key_exists("onchange",$this->hooks)) {
			foreach ($this->hooks["onchange"] as $func=>$object) {
				$object->{$func}($value);
			}
		}
	}
	
	function onclick() {
		if (array_key_exists("onclick",$this->hooks)) {
			foreach ($this->hooks["onclick"] as $func=>$object) {
				$object->{$func}();
			}
		}
	}
	
	function get() {}
	
	function update() {
		$answer = &$_SESSION['answer'];
        $answer[] = array('update',$this->uuid,$this->get());
	}
}

class oscombobox extends oscomponent {
	
	public $table;
	public $index;
	public $display;
	public $value;
	
	function __construct($parent) {
		parent::__construct($parent);
	}
	
	function onchange($value) {
		parent::onchange($value);
		$this->value = $value;
	}
	
	function get() {
		$ret = "<select id=".$this->uuid;
		$ret .= $this->active?"":" disabled=disabled";
		$ret .= " onchange=message(this.id,'onchange',this.value,0)";
		$ret .= ">";
		$ret .= "<option value='...' selected>...</option>";
		$this->table->first();
		if (!($this->table->count() == 0)) {
			do {
				$ret .= "<option value='".$this->table->{$this->display}."'>".$this->table->{$this->display}."</option>";
			} while ($this->table->next());
		}
		$ret .= "</select>";
		return $ret;
	}
}

class oswindow extends oscomponent {
	function __construct($parent) {
		parent::__construct($parent);
	}
	
}

class osbutton extends oscomponent {
	
	public $caption;
	public $accesskey;
	
	function __construct($parent) {
		parent::__construct($parent);
	}
	
	function get() {
		$ret = "<input id=".$this->uuid." type='button' value='".$this->caption."'";
		$ret .= $this->active?"":" disabled=disabled";
		$ret .= $this->accesskey?" accesskey='".$this->accesskey."'":"";
		$ret .= " onclick=message(this.id,'onclick',0,0)";
		$ret .= ">";
		return $ret;
	}
}

class osedit extends oscomponent {
	
	public $value;
	
	function __construct($parent) {
		parent::__construct($parent);
	}
	
	function onchange($value) {
		parent::onchange($value);
		$this->value = $value;
	}
	
	function get() {
		$ret = "<input id=".$this->uuid." type='text' value=''";
		$ret .= $this->active?"":" disabled=disabled";
		$ret .= " onchange=message(this.id,'onchange',this.value,0)";
		$ret .= ">";
		return $ret;
	}
}

class oslogin extends oscomponent {
	
	private $combi1;
	private $combi2;
	private $edit1;
	private $button1;
	private $username;
	private $userpassword;
	
	function __construct($parent) {
		parent::__construct($parent);
		$this->active = false;
		//$window = new oswindow(0);
		$this->combi1 = new oscombobox(0);
		$this->combi1->table = $_SESSION["data"]->dictionaries->Должности;//$data->dictionaries->Должности;
		$this->combi1->index = "id";
		$this->combi1->display = "name";
		$this->combi1->setonchange($this, "combi1onchange");
		$this->combi2 = new oscombobox(0);
		$this->combi2->table = $_SESSION["data"]->dictionaries->Пользователи;//Должности;//$data->dictionaries->Сотрудники;
		$this->combi2->index = "id";
		$this->combi2->display = "name";
		$this->combi2->disable();
		$this->edit1 = new osedit(0);
		$this->button1 = new osbutton(0);
		$this->button1->setevent($this, "onclick", "button1onclick");
		$this->button1->caption = "button";
		$this->button1->accesskey = "+";
	}
	
	function button1onclick() {
		$this->username = $this->combi2->value;
		$this->userpassword = $this->edit1->value;
		$this->enable();
		//$this->button1->disable();
	}
	
	function combi1onchange($value) {
		if(!($value == "...")) {
			//$this->combi2->table->searchbyfield("Должность", $value);
			$this->combi2->enable();
		} else {
			$this->combi2->disable();
		}
	}
	
	function get() {
		$ret = "<div id=".$this->uuid.">";
		if (!$this->active) {
			// Вывести экран входа
			$ret .= "Должность пользователя:<br>";
			$ret .= $this->combi1->get()."<br>";
			$ret .= "Имя пользователя:<br>";
			$ret .= $this->combi2->get()."<br>";
			$ret .= "Пароль пользователя:<br>";
			$ret .= $this->edit1->get()."<br>";
			$ret .= $this->button1->get();
		} else {
			// Вывести интерфейс пользователя
			//$dic = $_SESSION["data"]->dictionaries->Пользователи;
			//$dic->searchbyfield();
			$ret = "Интерфейс пользователя ".$this->userpassword;
		}
		$ret .= "</div>";
		return $ret;
	}
}
?>
